/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin		 	*/
/*	Project:	GlbRtbEng-1.0.0			*/
/*	Author:		gong_libin			*/
/*	Date:		2016_06_26			*/
/*	File:		GlbRtbMems.cpp			*/
/********************************************************/

#include "GlbRtbMems.h"

extern GLBRTBCONF_S g_stConf;

GLBRDCON_P GlbRtbMemsOpen()
{
	GlbNet::CGlbNet CNet;
	char* pszPort = NULL;
	GLBRDCON_P pstCon = NULL;
	char szHost[GLB_URL] = {0};

	strncpy(szHost, g_stConf.m_szReds, GLB_URL);
	if (NULL != (pszPort = CNet.GlbNetUrl(szHost))) {
		pstCon = GLB_RD_RCNT(szHost, atoi(pszPort));
		if (GLB_SUCCESS != GLB_RD_RERR(pstCon)) {
			GLB_ERROR("Failed to GLB_RD_RCNT\n");
			GlbRtbMemsClose(pstCon);
			GLB_RD_RNIL(pstCon);
		}
	}
	else {
		GLB_ERROR("Failed to GlbNetUrl\n");
	}

	return pstCon;
}

void GlbRtbMemsClose(GLBRDCON_P pstCon)
{
	GLB_RD_RFRE(pstCon);

	return;
}

void GlbRtbMemsCleanAll(GLBRDCON_P pstCon)
{
	GlbRdFlushAll(pstCon);

	return;
}

void GlbRtbMemsCleanDb(GLBRDCON_P pstCon, int iDb)
{
	GlbRdSelect(pstCon, iDb);
	GlbRdFlushdb(pstCon);

	return;
}

int GlbRtbMemsCreate(GlbCls::CGlbSet& rCSet)
{
	UINT uiInfo = 0;
	GLBRDCON_P pstCon = NULL;
	GLBRAINFO_S* pstInfo = NULL;

	if (NULL != (pstCon = GlbRtbMemsOpen())) {
		GlbRtbMemsCleanDb(pstCon, GLB_RM_ADVS);
		while (uiInfo < rCSet.GlbSetGetCount()) {
			pstInfo = (GLBRAINFO_S*)rCSet.GlbSetIndexGet(uiInfo ++);
			GLB_RD_RFRO(GLB_RD_RCMD(pstCon, "HMSET %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
				pstInfo->m_szAdId,
				GLB_RD_BGTM, pstInfo->m_szBgTm,
				GLB_RD_EDTM, pstInfo->m_szEdTm,
				GLB_RD_BROW, pstInfo->m_szBrow,
				GLB_RD_OSNM, pstInfo->m_szOsNm,
				GLB_RD_LOCA, pstInfo->m_szLoca,
				GLB_RD_MDTY, pstInfo->m_szMdTy,
				GLB_RD_MEDI, pstInfo->m_szMedi,
				GLB_RD_PRIO, pstInfo->m_szPrio,
				GLB_RD_VOLU, pstInfo->m_szVolu,
				GLB_RD_PRIC, pstInfo->m_szPric,
				GLB_RD_CRUL, pstInfo->m_szCUrl,
				GLB_RD_SCRN, pstInfo->m_szSrcn,
				GLB_RD_TYPE, pstInfo->m_szType
			));
		}
		GlbRtbMemsClose(pstCon);
	}
	else {
		return GLB_FAILURE;
	}

	return GLB_SUCCESS;
}

void GlbRtbMemsSelect(GLBRDCON_P pstCon, char* pszID, GLBRAINFO_S* pstInfo)
{
	UINT uiCnt = 0;
	GLBRDREP_P pstRep = NULL;
	GLBRMKV_S stKv[] = {
		{GLB_RD_BGTM, pstInfo->m_szBgTm},
		{GLB_RD_EDTM, pstInfo->m_szEdTm},
		{GLB_RD_BROW, pstInfo->m_szBrow},
		{GLB_RD_OSNM, pstInfo->m_szOsNm},
		{GLB_RD_LOCA, pstInfo->m_szLoca},
		{GLB_RD_MDTY, pstInfo->m_szMdTy},
		{GLB_RD_MEDI, pstInfo->m_szMedi},
		{GLB_RD_PRIO, pstInfo->m_szPrio},
		{GLB_RD_VOLU, pstInfo->m_szVolu},
		{GLB_RD_PRIC, pstInfo->m_szPric},
		{GLB_RD_CRUL, pstInfo->m_szCUrl},
		{GLB_RD_SCRN, pstInfo->m_szSrcn},
		{GLB_RD_TYPE, pstInfo->m_szType},
	};

	if (NULL != (pstRep = (GLBRDREP_P)GLB_RD_RCMD(pstCon, "HMGET %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
			pszID,
			stKv[0].m_pszKey,
			stKv[1].m_pszKey,
			stKv[2].m_pszKey,
			stKv[3].m_pszKey,
			stKv[4].m_pszKey,
			stKv[5].m_pszKey,
			stKv[6].m_pszKey,
			stKv[7].m_pszKey,
			stKv[8].m_pszKey,
			stKv[9].m_pszKey,
			stKv[10].m_pszKey,
			stKv[11].m_pszKey,
			stKv[12].m_pszKey
	))) {
		for (uiCnt = 0; uiCnt < GLB_RD_RTTL(pstRep); uiCnt ++) {
			strncpy(stKv[uiCnt].m_pszVal, GLB_RD_RVAL(pstRep, uiCnt), GLB_RD_RLEN(pstRep, uiCnt));
		}
		GLB_RD_RFRO(pstRep);
	}

	return;
}

void GlbRtbMemsShow()
{
	char* pszKey = NULL;
	GLBRDCON_P pstCon = NULL;
	UINT uiKey = 0, uiVal = 0;
	GLBRDREP_P pstKey = NULL, pstVal = NULL;
	char* pszFld[] = {
		GLB_RD_BGTM,
		GLB_RD_EDTM,
		GLB_RD_BROW,
		GLB_RD_OSNM,
		GLB_RD_LOCA,
		GLB_RD_MDTY,
		GLB_RD_MEDI,
		GLB_RD_PRIO,
		GLB_RD_VOLU,
		GLB_RD_PRIC,
		GLB_RD_CRUL,
		GLB_RD_SCRN,
		GLB_RD_TYPE
	};

	if (NULL != (pstCon = GlbRtbMemsOpen())) {
		if (NULL != (pstKey = (GLBRDREP_P)GlbRdScan(pstCon))) {
			while (uiKey < GLB_RD_RTTL(pstKey)) {
				pszKey = GLB_RD_RVAL(pstKey, uiKey);
				GLB_PRINT("Key[%d]: %s\n", uiKey ++, pszKey);
				if (NULL != (pstVal = (GLBRDREP_P)GLB_RD_RCMD(pstCon, "HMGET %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
						pszKey,
						pszFld[0],
						pszFld[1],
						pszFld[2],
						pszFld[3],
						pszFld[4],
						pszFld[5],
						pszFld[6],
						pszFld[7],
						pszFld[8],
						pszFld[9],
						pszFld[10],
						pszFld[11],
						pszFld[12]
					))) {
					for (uiVal = 0; uiVal < GLB_RD_RTTL(pstVal); uiVal ++) {
						GLB_PRINT("%s: %s\n", pszFld[uiVal], GLB_RD_RVAL(pstVal, uiVal));
					}
					GLB_RD_RFRO(pstVal);
				}
				GLB_PRINT("\n");
			}
			GLB_RD_RFRO(pstKey);
		}
		GlbRtbMemsClose(pstCon);
	}

	return;
}
